// 配置路由的地方
import Vue from "vue";
import VueRouter from "vue-router";
import Router from "vue-router";

// 使用插件
Vue.use(Router);

// 引入路由组件
import Home from "@/pages/Home";
import Search from "@/pages/Search";
import Login from "@/pages/Login";
import Register from "@/pages/Register";
// 先把VueRouter原型对象方法push，保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

// 重写push replace
// 第一个参数localtion 告诉路由跳转地址，success：成功回调 error：失败回调
VueRouter.prototype.push = function (localtion, success, error) {
  if (success && error) {
    originPush.call(this, localtion, success, error);
  } else {
    originPush.call(
      this,
      localtion,
      () => {},
      () => {}
    );
  }
};

VueRouter.prototype.replace = function (localtion, success, error) {
  if (success && error) {
    originReplace.call(this, localtion, success, error);
  } else {
    originReplace.call(
      this,
      localtion,
      () => {},
      () => {}
    );
  }
};

export default new VueRouter({
  // 配置路由
  routes: [
    {
      path: "/home",
      component: Home,
      meta: { show: true },
    },
    {
      path: "/search/:keyword?",
      component: Search,
      meta: { show: true },
      name: "search",
    },
    {
      path: "/login",
      component: Login,
      meta: { show: false },
    },
    {
      path: "/register",
      component: Register,
      meta: { show: false },
    },
    // 重定向 在项目运行之后，访问/ 立马让他重定向到首页
    {
      path: "*",
      redirect: "/home",
    },
  ],
});
